package com.zhengsanming.service;

import com.zhengsanming.mapper.BrandMapper;
import com.zhengsanming.pojo.Brand;
import com.zhengsanming.pojo.PageBean;
import com.zhengsanming.util.SqlsessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlsessionFactoryUtils.getSqlSessionFactory();

    /*
     * 查询所有
     *
     *  */
    public List<Brand> selectAll() {
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;
    }

    /*
     * 添加
     * 1、*/
    public void add(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    /*
     * 查询一个
     * */
    public Brand selectById(int id) {
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand brands = mapper.selectByIdBrand(id);
        sqlSession.close();
        return brands;
    }

    /*
     * 修改
     * */
    public void update(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.update(brand);
        sqlSession.commit();
        sqlSession.close();
    }

    /*
     * 批量删除
     * */
    public void deleteByIds(int[] ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        mapper.deleteByIds(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    //分页查询
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        //计算查询条目数
        int size = pageSize;
        //查询当前页数据
        List<Brand> rows = mapper.selectByPage(begin, size);
        //总记录数
        int totalCount = mapper.selectTotalCount();
        PageBean<Brand> pageBean = new PageBean<Brand>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        sqlSession.close();
        return pageBean;

    }

    //条件查询
    public PageBean<Brand> selectPageAndCondition(int currentPage, int pageSize, Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取BrandMapper对象
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算开始索引
        //4. 计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;

        // 处理brand条件，模糊表达式
        String brandName = brand.getBrandName();
        if (brandName != null && brandName.length() > 0) {
            brand.setBrandName("%" + brandName + "%");
        }

        String companyName = brand.getCompanyName();
        if (companyName != null && companyName.length() > 0) {
            brand.setCompanyName("%" + companyName + "%");
        }


        //5. 查询当前页数据
        List<Brand> rows = mapper.selectPageAndCondition(begin, size, brand);

        //6. 查询总记录数
        int totalCount = mapper.selectTotalandCondition(brand);

        //7. 封装PageBean对象
        PageBean<Brand> pageBean = new PageBean<Brand>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);


        //8. 释放资源
        sqlSession.close();

        return pageBean;
    }
}
